/**
 * Created by 12613 on 2016/8/13.
 */
//回到顶部按钮
$(document).ready(function () {
    //回到顶部
    $.goup({
        trigger: 80,
        bottomOffset: 80,
        locationOffset: 100,
        containerRadius:0,
        hideUnderWidth:767,
        containerColor:'#336699',
        arrowColor:'#fff',
        title: '',
        entryAnimation:'slide',
        titleAsText: true,
        goupSpeed:"fast"
    });
    //导航
    var m = '<div class="container"><div class="navbar-header"><button type="button" class="navbar-toggle" data-toggle="collapse"                data-target="#jframe-navbar-collapse"><span class="sr-only">切换导航</span><span class="icon-bar"></span><span class="icon-bar"></span><span class="icon-bar"></span></button><a class="navbar-brand" href="./index.html">JFrame.cn</a></div><div class="collapse navbar-collapse" id="jframe-navbar-collapse"><ul class="nav navbar-nav"><li ><a href="./index.html">文章</a></li><li><a href="./index-album.html">专栏</a></li></ul><ul class="nav navbar-nav jf-nav-right" ><li><a id="login-btn" href="javascript:0;">登录</a></li><li><img src="img/7.jpg" class="img-circle dropdown-toggle"  data-toggle="dropdown" style="width: 40px;height: 40px;margin: 5px 15px"><ul class="dropdown-menu"><li><a href="#">新建文章</a></li><li><a href="#">新建专辑</a></li><li class="divider hidden-xs"></li><li><a href="#">主页</a></li><li><a href="#">个人设置</a></li></ul></li></ul></div></div>';
    $("#jf-head-nav").append(m);


});

<!--编辑器-->
$("#art-editor").markdown({
    language: 'zh',
    fullscreen: {
        enable: true
    },
    height:676,
    resize: 'vertical',
    localStorage: 'md',
    imgurl: '/c/imgUpload',//图像上传地址
    base64url: '/c/imgUpload',//base64编码格式图像上传地址
    autofocus:false,
    savable:true,
    onSave: function(e) {
        alert("Saving '"+e.getContent()+"'...");
        alert("Saving '"+e.parseContent()+"'...");
        var txtTitle = $("#art-title").val();
        alert("title==>"+txtTitle);
    }
});

//标签添加相关逻辑


$("#add-tag").click(function(event){
    $("#add-tag").hide();
    $("#tags-dropdown").css("display","inline-block");
    $("#tag-input").val("");
    $("#tag-input").focus();
    //防止事件冒泡
    event.stopPropagation();
});

function Tag(id,name){
    this.name = name;
    this.id = id;
}

//给输入框注册点击事件处理，防止事件冒泡
$('#tag-input').click(function(event){
    // donothing
    //防止在点击tag-input时触发点击docment事件
    event.stopPropagation();
});
//输入时联想
$('#tag-input').bind('input propertychange',
    function() {
        $("#tags-dropdown").addClass("open");//显示下拉列表

        //todo 获取数据，这个数据会在文档加载时添加到网页中。
        var tagList = [
            {id:1,name:"spring"},
            {id:2,name:"java"},
            {id:3,name:"数据库"},
            {id:4,name:"slf4j"},
            {id:4,name:"IOS"},
            {id:4,name:"安卓"}
        ];

        //与输入匹配的标签
        var nameKey = $('#tag-input').val();
        var matchedTagList = searchMatchedTags(tagList,nameKey);

        freshTheTagsDropdown(matchedTagList);
    }
);

//点击空白处隐藏弹出菜单
$(document).click(showAddTagThenHideTagInput);

function removeTagEventBind(){
    $(".jf-tag-remove").click(function(event){
        var a = this;
        $(this.parentElement).hide();
        event.stopPropagation();
    });
}
removeTagEventBind();


//隐藏tag输入框，显示【+ 添加标签】
function showAddTagThenHideTagInput(){
    $("#tags-dropdown").hide();
    var addTagObj =  $("#add-tag");
    addTagObj.show();
    //清除下拉菜单内容
    clearTagsDropdown();
}
function clearTagsDropdown(){
    $("#tags-dropdown-menu").html("");
    $("#tags-dropdown").removeClass("open");
}

function freshTheTagsDropdown(tagList){
    if(tagList == null || tagList == undefined){
        return;
    }
    if(tagList.length > 0){
        //添加元素到菜单栏
        var tagListHtml = "";
        for(var i = 0 ;i < tagList.length;i++){
            // html:<li role="presentation"><a role="menuitem"  tabindex="-1" href="#">数据挖掘</a></li>
            var html = '<li data-id="'+tagList[i].id+'" role="presentation"><a role="menuitem"  tabindex="-1" href="#">'+tagList[i].name+'</a></li>';
            tagListHtml = tagListHtml + "\n" + html;
        }
        $("#tags-dropdown-menu").html(tagListHtml);
    } else {
        //添加 【创建新的标签】
        // html: <li role="presentation"><a role="menuitem" class="jf-add-tag" tabindex="-1" href="#"><span class="glyphicon glyphicon-plus-sign" aria-hidden="true">&nbsp;</span>添加标签</a></li>
        var html = '<li id="jf-create-tag-li" role="presentation"><a role="menuitem" id="jf-create-tag" class="jf-add-tag" tabindex="-1" href="javascript:0;">' +
            '<span class="glyphicon glyphicon-send" aria-hidden="true">&nbsp;</span>新的标签</a></li>';
        $("#tags-dropdown-menu").html(html);
        //单击【+ 新的标签】则创建新的标签
        $("#jf-create-tag-li").click(createNewTag);

    }
}
// todo 通过名字搜索tagList
function searchMatchedTags(tagList, nameKey) {
    //return [{id:4,name:"安卓"}];
    return [];
}


function createNewTag(){
    //去tag-inpt的值作为name
    var tagName = $('#tag-input').val();
    // todo post到服务器
    alert("保存tag["+tagName+"]成功");
    var tag = new Tag(1,tagName);
    //添加到选择标签box中
    addOneTagInJFTagBox(tag);
    //将新的tag保存到本地内存
    putOneTagOnLocal(tag)
}
//将tag保存到内存tagList中
function putOneTagOnLocal(tag){
    //
}
//将tag保存到TagBox中
function addOneTagInJFTagBox(tag){
    //html:<span class="jf-tag">spring mvc<a href="#" class="jf-tag-remove">×</a></span>
    var html = '\n <span class="jf-tag" tag-id="'+tag.id+'">'+tag.name+'<a href="javascript:0;" class="jf-tag-remove">×</a></span>';
    $("#tags-dropdown").before(html);
    //绑定删除事件
    removeTagEventBind();
}


//tag添加时间
